using UnityEngine;
using System.Collections;

public class FadeOutTime : MonoBehaviour {

    public float timeLife;
    private bool ready;
    private float i = 1;
	// Use this for initialization
	IEnumerator Start () {
        yield return new WaitForSeconds(timeLife);
        ready = true;
	}
	
	// Update is called once per frame
	void Update () {
        if (ready)
        {
            i -= 3 * Time.deltaTime;
            fade();
            if (i < 0)
            {
                Destroy(gameObject);
            }
        }
	}

    void fade()
    { 
        Transform t = transform;
        foreach (Transform p in t.transform)
            p.renderer.material.color = new Color(p.renderer.material.color.r, p.renderer.material.color.g, p.renderer.material.color.b, i);
    }
}
